package com.tronsis.distribution.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tronsis.distribution.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class TypeAdapter extends BaseAdapter {

    private Context context;
    private List<String> list;
    private int checkItemPosition = 0;
    private GridView mGridView;

    public void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataSetChanged();
    }

    public TypeAdapter(Context context, List<String> list,GridView gridView) {
        this.context = context;
        this.list = list;
        this.mGridView = gridView;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_constellation_layout, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
            viewHolder.update(mGridView);

        }
        fillValue(position, viewHolder);

        viewHolder.mText.setTag(position);
        viewHolder.mLlItem.setTag(convertView);

        return convertView;
    }

    private void fillValue(int position, ViewHolder viewHolder) {
        viewHolder.mText.setText(list.get(position));
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.mText.setTextColor(context.getResources().getColor(R.color.white));
                viewHolder.mText.setBackgroundResource(R.drawable.btn_normal_red_bg);
            } else {
                viewHolder.mText.setTextColor(context.getResources().getColor(R.color.text_normal));
                viewHolder.mText.setBackgroundResource(R.drawable.btn_normal_white_bg);
            }
        }
    }

    static class ViewHolder {
        @BindView(R.id.text)
        TextView mText;
        @BindView(R.id.ll_item)
        LinearLayout mLlItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }


        public void update(final GridView gv) {

// 精确计算GridView的item高度

            mText.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        public void onGlobalLayout() {
                            int position = (Integer) mText.getTag();
// 这里是保证同一行的item高度是相同的！！也就是同一行是齐整的 height相等
                            if (position > 0) {
                                View v = (View) mLlItem.getTag();
                                int height = v.getHeight();

                                View view = gv.getChildAt(position - 1);
                                if (view == null) {
                                    return;
                                }
                                int lastheight = view.getHeight();

// 得到同一行的最后一个item和前一个item想比较，把谁的height大，就把两者中
// height小的item的高度设定为height较大的item的高度一致，也就是保证同一
// // 行高度相等即可

                                if (height > lastheight) {
                                    view.setLayoutParams(new GridView.LayoutParams(
                                            GridView.LayoutParams.FILL_PARENT,
                                            height));
                                } else if (height < lastheight) {
                                    v.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.FILL_PARENT, lastheight));
                                }
                            }
                        }

                    });
        }

    }
}